From 91e40657ae822a14adad6fdc5d4fc4b6061f3b1c Mon Sep 17 00:00:00 2001 From: Stephen Becker IV Date: Sun, 15 May 2016 22:44:30 -0700 Subject: [PATCH] An author by another name Dearest Reviewer This is to support environment variables for name and email more like git. This closes #1213 . I did look in to using libgit2 but I did not see a clear way to get the author ident. This just moves from or_else chaining to map_filter with env checks and getting the nth(0) result. The docs I found https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables#Committing do not really indicate an order of preference to the variables. I am more then happy to rearrange the arrays in anyway. Thanks for reviewing, Becker --- src/cargo/ops/cargo_new.rs | 15 ++++++++++++--- tests/test_cargo_new.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index f125622c9..2a878452c 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -435,12 +435,20 @@ mod tests { Ok(()) } +fn get_environment_variable(variables: &[&str] ) -> Option{ + variables.iter() + .filter_map(|var| env::var(var).ok()) + .next() +} + fn discover_author() -> CargoResult<(String, Option)> { let git_config = GitConfig::open_default().ok(); let git_config = git_config.as_ref(); + let name_variables = ["NAME", "GIT_AUTHOR_NAME", "GIT_COMMITTER_NAME", + "USER", "USERNAME"]; let name = git_config.and_then(|g| g.get_string("user.name").ok()) - .or_else(|| env::var("USER").ok()) // unix - .or_else(|| env::var("USERNAME").ok()); // windows + .or_else(|| get_environment_variable(&name_variables)); + let name = match name { Some(name) => name, None => { @@ -449,8 +457,9 @@ fn discover_author() -> CargoResult<(String, Option)> { username_var) } }; + let email_variables = ["EMAIL", "GIT_AUTHOR_EMAIL", "GIT_COMMITTER_EMAIL"]; let email = git_config.and_then(|g| g.get_string("user.email").ok()) - .or_else(|| env::var("EMAIL").ok()); + .or_else(|| get_environment_variable(&email_variables) ); let name = name.trim().to_string(); let email = email.map(|s| s.trim().to_string()); diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index 4a73925c2..8a4330d65 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -203,6 +203,37 @@ test!(finds_author_git { assert!(contents.contains(r#"authors = ["bar "]"#)); }); +test!(finds_git_email{ + let td = TempDir::new("cargo").unwrap(); + assert_that(cargo_process("new").arg("foo") + .env("GIT_AUTHOR_NAME", "foo") + .env("GIT_AUTHOR_EMAIL", "gitfoo") + .cwd(td.path().clone()), + execs().with_status(0)); + + let toml = td.path().join("foo/Cargo.toml"); + let mut contents = String::new(); + File::open(&toml).unwrap().read_to_string(&mut contents).unwrap(); + assert!(contents.contains(r#"authors = ["foo "]"#), contents); +}); + + +test!(finds_git_author{ + // Use a temp dir to make sure we don't pick up .cargo/config somewhere in + // the hierarchy + let td = TempDir::new("cargo").unwrap(); + assert_that(cargo_process("new").arg("foo") + .env_remove("USER") + .env("GIT_COMMITTER_NAME", "gitfoo") + .cwd(td.path().clone()), + execs().with_status(0)); + + let toml = td.path().join("foo/Cargo.toml"); + let mut contents = String::new(); + File::open(&toml).unwrap().read_to_string(&mut contents).unwrap(); + assert!(contents.contains(r#"authors = ["gitfoo"]"#)); +}); + test!(author_prefers_cargo { ::process("git").args(&["config", "--global", "user.name", "foo"]) .exec().unwrap(); -- 2.30.2